<template lang="pug">
  d2-container(:filename="filename")
    el-card(shadow="never", style="width:40%;margin:auto")
      div(slot="header") 修改密码
      el-form(label-width="100px",:model="newPass",:rules="rules",ref="form")
        el-form-item(label="旧密码",prop="oldPass")
          el-input(type="text",v-model="newPass.oldPass")
        el-form-item(label="新密码",prop="newPass1")
          el-input(type="password",v-model="newPass.newPass1")
        el-form-item(label="新密码确认",prop="newPass2")
          el-input(type="password",v-model="newPass.newPass2")
        el-form-item(label="")
          submit-button(type="default",size="default",@click="submit")
            d2-icon(name="save") 
            span &nbsp;提交修改

</template>

<script>
/**
 * 用户中心 修改密码
 */
import axios from "@/plugin/axios";
import Base from "@/mixins/Base";
export default {
  name: "center-password",
  mixins: [Base],
  data() {
    var validateNewPass2 = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.newPass.newPass1) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };

    return {
      filename: __filename,
      newPass: {
        oldPass: null,
        newPass1: null,
        newPass2: null
      },
      rules: {
        oldPass: [{ required: true, message: "请输入旧密码" }],
        newPass1: [{ required: true, message: "请输入旧密码" }],
        newPass2: [
          { validator: validateNewPass2, trigger: "blur" },
          { required: true, message: "请再次输入密码", trigger: "blur" }
        ]
      },
      submiting: false
    };
  },
  methods: {
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) this.doSubmit();
      });
    },
    doSubmit() {
      if (this.submiting) return;
      this.submiting = true;
      axios({
        method: "post",
        url: "Admin/UpdatePass",
        data: { model: this.newPass }
      })
        .then(() => this.submitSuccess())
        .catch(() => (this.submiting = false));
    },
    submitSuccess() {
      this.submiting = false;
      this.showUpdateSuccess();
    }
  }
};
</script>

<style>
</style>